package com.example.cms.service.Impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.example.cms.pojo.FileExport;
import com.example.cms.pojo.Mesagge;
import com.example.cms.pojo.QueryPage;
import com.example.cms.until.ExcelUitl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.cms.dao.CustomerDao;
import com.example.cms.entity.Customer;
import com.example.cms.service.CustomerService;

@Service("CustomerService")
@Transactional
public class CustomerServiceImpl implements CustomerService{
	
	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
     private CustomerService cusSer;
	
	@Override
	public List<Customer> getAllCus() {
		// TODO Auto-generated method stub
		return customerDao.getAllCus();
	}

	@Override
	public List<Customer> getByPageCus(Integer page) {
		int pagesize = cusSer.countPageSize();//得到总页码数
		List<Customer> list = new ArrayList<Customer>();
		if(page > pagesize) {
			System.out.println("页码过大");
		}else {
			System.out.println("页码合适");
	    	int i=	customerDao.getDataNum();
			list = customerDao.getAllCusBySize((page - 1) * pageSize, pageSize);

		}
		return list;
	}
	@Override
	public Integer addCustomer(Customer customer) {
	    System.out.println(customer.toString());
	Customer customer1=customerDao.selectCustomer(customer.getCustomerPhone());
		if (customer!=null){
            System.out.println(12);
			return customerDao.addCus(customer);
		}else {
            System.out.println(1);
			return 0;
		}
	}
	@Override
	public Integer updateCustomer(Customer customer) {
		return customerDao.updateCus(customer);
	}

    @Override
    public Mesagge NumDataNum(Mesagge mesagge) {
       int i = customerDao.getDataNum();
       //设置里面的数据条数
       mesagge.setMesCode(i);
       return  mesagge ;
    }

	@Override
	public Integer deletCustomer(Customer customer) {
		return customerDao.deleteCus(customer.getCustomerId());
	}

	@Override
	public List<Customer> getAllCusAndOr() {
		return customerDao.getAllCusAndOr();
	}
	@Override
	public  Integer countPageSize() {
		int size = customerDao.getDataNum();
		int pageNum = 0;//页码数
		if(size % pageSize == 0) {
			pageNum = size / pageSize;
		}else {
			pageNum = size / pageSize + 1;
		}
		return pageNum;
	}
	@Override
	public List<Customer> LikeName(QueryPage queryPage, Mesagge mes) {
		mes.setMesCode(customerDao.slectLikeCusNum(queryPage.getCustomerName()));
		System.out.println(queryPage.getPageSize());
		System.out.println(queryPage.getCurr_page());
		System.out.println(queryPage.getCustomerName());
		List <Customer> customers=	customerDao.selectLikeCusName(queryPage.getCustomerName(),(queryPage.getCurr_page()-1)*queryPage.getPageSize(),queryPage.getPageSize());
		System.out.println(customers);
		return customers;
	}

	/**
	 * @Author Sunbird
	 * @Description //客户数据exec导出
	 * @Date  2018/12/13
	 * @Param
	 * @return
	 **/
	@Override
	public FileExport exportUserDTO(QueryPage queryPage) throws Exception{
		//分页查询客户数据
		List<Customer> customers=getAllCus();
		//解析数据库数据 转换数据格式
		SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
		FileExport fileExport =new FileExport();
		List<FileExport> fileExports =new ArrayList<FileExport>();
		String [] state ={"未激活","激活"};
		//添加表名
		fileExport.setName("客户导出数据");
		//添加表头
		List<String> titles = new ArrayList();
		titles.add("客户名称");
		titles.add("客户公司");
		titles.add("客户电话");
		titles.add("公司地址");
		titles.add("客户创建时间");
		titles.add("客户状态");
		fileExport.setTitles(titles);
		//添加数据
        List<List<Object>> rows = new ArrayList();
        List<Object> c ;
		for (int i=0; i< customers.size();i++){
          c=new ArrayList<Object>();
           c.add(customers.get(i).getCustomerName());
            c.add(customers.get(i).getCustomerCompany());
            c.add(customers.get(i).getCustomerPhone());
            c.add(customers.get(i).getCustomerAddre());
            c.add(customers.get(i).getCustomerDate());
            c.add(state[customers.get(i).getCustomerState()]);
            rows.add(c);
		}

        fileExport.setRows(rows);
        return fileExport;
	}
}
